using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScannerLib.Tsai;

namespace Desktop_Scanner
{

    

    public partial class LightCalibrationWindow : Control
    {
        private Bitmap m_bitmap;
        private List<PointPair> m_calibrationPoints = new List<PointPair>();
        
        private PointPair m_selectedPoint;
        private bool m_changingStart = false;
        private bool m_changingEnd = false;

        public LightCalibrationWindow()
        {
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.DoubleBuffered = true;
            InitializeComponent();
        }

        public bool Changing
        {
            get
            {
                return m_changingEnd || m_changingStart;
            }
        }

        public bool ChangingEnd
        {
            get
            {
                return m_changingEnd;
            }

            set
            {
                m_changingEnd = value;
            }
        }

        public bool ChangingStart
        {
            get
            {
                return m_changingStart;
            }

            set
            {
                m_changingStart = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public PointPair SelectedPoint
        {
            get
            {
                return m_selectedPoint;
            }

            set
            {
                m_selectedPoint = value;
                OnSelectedPointChanged(EventArgs.Empty);
            }
        }

        public Bitmap Bitmap
        {
            get
            {
                return m_bitmap;
            }

            set
            {
                m_bitmap = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<PointPair> CalibrationPoints
        {
            get
            {
                return m_calibrationPoints;
            }

            set
            {
                m_calibrationPoints = value ?? new List<PointPair>();
                OnSelectedPointChanged(EventArgs.Empty);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            pe.Graphics.FillRectangle(Brushes.Black, ClientRectangle);

            //try 
            try
            {
                if (m_bitmap != null)
                {

                    pe.Graphics.DrawImage(m_bitmap, ClientRectangle);

                }
                if (CalibrationPoints != null && m_bitmap != null)
                {
                    foreach (PointPair p in CalibrationPoints)
                    {
                        Pen penStart = p == m_selectedPoint ? Pens.Yellow : Pens.White;
                        Pen penEnd = p == m_selectedPoint ? Pens.HotPink : Pens.Gray;

                        int px1 = (int)((p.Start.X * Width) / m_bitmap.Width);
                        int py1 = (int)((p.Start.Y * Height) / m_bitmap.Height);

                        int px2 = (int)((p.End.X * Width) / m_bitmap.Width);
                        int py2 = (int)((p.End.Y * Height) / m_bitmap.Height);

                        pe.Graphics.DrawRectangle(penStart, new Rectangle(px1 - 5, py1 - 5, 11, 11));
                        pe.Graphics.DrawLine(penStart, px1 - 5, py1, px1 + 5, py1);
                        pe.Graphics.DrawLine(penStart, px1, py1 - 5, px1, py1 + 5);

                        pe.Graphics.DrawRectangle(penEnd, new Rectangle(px2 - 5, py2 - 5, 11, 11));
                        pe.Graphics.DrawLine(penEnd, px2 - 5, py2, px2 + 5, py2);
                        pe.Graphics.DrawLine(penEnd, px2, py2 - 5, px2, py2 + 5);
                    }
                }

            }
            catch { }
            // Calling the base class OnPaint
            base.OnPaint(pe);
        }

        public event EventHandler SelectedPointChanged;
        protected virtual void OnSelectedPointChanged(EventArgs e)
        {
            if (SelectedPointChanged != null)
            {
                SelectedPointChanged(this, e);
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (m_bitmap == null)
                return;

            if (m_changingStart && m_selectedPoint != null)
            {
                m_selectedPoint.Start.X = (e.X * m_bitmap.Size.Width) / this.Size.Width;
                m_selectedPoint.Start.Y = (e.Y * m_bitmap.Size.Height) / this.Size.Height;
                Refresh();
            }
            else if (m_changingEnd && m_selectedPoint != null)
            {
                m_selectedPoint.End.X = (e.X * m_bitmap.Size.Width) / this.Size.Width;
                m_selectedPoint.End.Y = (e.Y * m_bitmap.Size.Height) / this.Size.Height;
                Refresh();
            }
            else
            {
                foreach (PointPair cp in m_calibrationPoints)
                {
                    int px1 = (int)((cp.Start.X * Width) / m_bitmap.Width);
                    int py1 = (int)((cp.Start.Y * Height) / m_bitmap.Height);

                    int px2 = (int)((cp.End.X * Width) / m_bitmap.Width);
                    int py2 = (int)((cp.End.Y * Height) / m_bitmap.Height);

                    if (e.X > px1 - 5 && e.X < px1 + 5 && e.Y > py1 - 5 && e.Y < py1 + 5)
                    {
                        SelectedPoint = cp;
                        Refresh();
                        break;
                    }

                    if (e.X > px2 - 5 && e.X < px2 + 5 && e.Y > py2 - 5 && e.Y < py2 + 5)
                    {
                        SelectedPoint = cp;
                        Refresh();
                        break;
                    }
                }
            }
            
        }
    }
}
